﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class SysMenuDao
    {
        public bool Delete(SqlConnection conn, string menuId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Sys_Function] ");
            builder.Append("WHERE ");
            builder.Append("\tLeft([MenuId],2) = @MenuId ");
            builder.Append("DELETE FROM [dbo].[Sys_Menu] ");
            builder.Append("WHERE ");
            builder.Append("\t[MenuId] = @MenuId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuId", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = menuId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public bool Insert(SqlConnection conn, SysMenuData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Sys_Menu]  ");
            builder.Append("( ");
            builder.Append("\t[MenuId], ");
            builder.Append("\t[MenuName], ");
            builder.Append("\t[Image] ");
            builder.Append(")  ");
            builder.Append("VALUES  ");
            builder.Append("( ");
            builder.Append("\t@MenuId, ");
            builder.Append("\t@MenuName, ");
            builder.Append("\t@Image ");
            builder.Append(") ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@MenuId", SqlDbType.NVarChar, 50);
            commandParameters[0].Value = data.MenuId;
            commandParameters[1] = new SqlParameter("@MenuName", SqlDbType.NVarChar, 100);
            commandParameters[1].Value = data.MenuName;
            commandParameters[2] = new SqlParameter("@Image", SqlDbType.NVarChar, 100);
            commandParameters[2].Value = data.Image;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[MenuId], ");
            builder.Append("\t[MenuName], ");
            builder.Append("\t[Image] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Sys_Menu] ");
            builder.Append("ORDER BY ");
            builder.Append("\t[MenuId] ASC ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Sys_Menu";
            return set.Tables[0];
        }

        public DataTable SelectByMenuId(SqlConnection conn, string menuId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[MenuId], ");
            builder.Append("\t[MenuName], ");
            builder.Append("\t[Image] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Sys_Menu] ");
            builder.Append("WHERE ");
            builder.Append("\t[MenuId] = @MenuId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MenuId", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = menuId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Sys_Menu";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, SysMenuData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Sys_Menu] ");
            builder.Append("SET ");
            builder.Append("\t[MenuName] = @MenuName, ");
            builder.Append("\t[Image] = @Image ");
            builder.Append("WHERE ");
            builder.Append("\t[MenuId] = @MenuId ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@MenuId", SqlDbType.NVarChar, 50);
            commandParameters[0].Value = data.MenuId;
            commandParameters[1] = new SqlParameter("@MenuName", SqlDbType.NVarChar, 100);
            commandParameters[1].Value = data.MenuName;
            commandParameters[2] = new SqlParameter("@Image", SqlDbType.NVarChar, 100);
            commandParameters[2].Value = data.Image;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

